package personnel.department.pages;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import personnel.department.dao.entity.Employee;

public class EmployeeInfoComposite extends Composite {

	private Label certificates;
	private Label degree;
	private Label qualifications;
	private Label retrainings;
	private Label validations;
	private Label fullName;
	private Label post;
	private Label internship;
	private String internAndTrain;
	private String currentDegree;

	public EmployeeInfoComposite(Composite parent, int style) {
		super(parent, style);
		createContent();
	}

	private void createContent() {
		setBackground(JFaceColors.getBannerBackground(getDisplay()));
		GridLayoutFactory.swtDefaults().applyTo(this);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
		
		Label title = new Label(this, SWT.NONE);
		title.setText("Информация о сотруднике");
		title.setBackground(this.getBackground());
		GridDataFactory.swtDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(title);
		
		fullName = new Label(this, SWT.NONE);
		fullName.setBackground(this.getBackground());
		
		post = new Label(this, SWT.NONE);
		post.setBackground(this.getBackground());
		
		internship = new Label(this, SWT.NONE);
		internship.setBackground(this.getBackground());
		
		degree = new Label(this, SWT.NONE);
		degree.setBackground(this.getBackground());
		
		certificates = new Label(this, SWT.NONE);
		certificates.setBackground(this.getBackground());
		
		qualifications = new Label(this, SWT.NONE);
		qualifications.setBackground(this.getBackground());
		
		retrainings = new Label(this, SWT.NONE);
		retrainings.setBackground(this.getBackground());
		
		validations = new Label(this, SWT.NONE);
		validations.setBackground(this.getBackground());
		clear();
	}

	public void clear() {
		fullName.setText("ФИО: ");
		post.setText("Должность: ");
		internship.setText("Окончил: ");
		degree.setText("Ученая степень: ");
		certificates.setText("Количество сертификатов: ");
		qualifications.setText("Количество квалификаций: ");
		retrainings.setText("Число переподготовок: ");
		validations.setText("Число аттестаций: ");
		fullName.pack();
		post.pack();
		internship.pack();
		degree.pack();
		certificates.pack();
		qualifications.pack();
		retrainings.pack();
		validations.pack();
	}
	
	public void updateInfo(Employee employee) {
		String fio = employee.getSurname() + " " + employee.getFirstName() + " " + employee.getSecondName();
		setValue(fullName, fio);
		
		setValue(post, employee.getType().getPost());
		
		internAndTrain = "";
		employee.getEducation().getInternship().forEach(intern -> {
			if (intern.getInternship()) {
				internAndTrain = internAndTrain.isEmpty() ? "интернатура" : internAndTrain + ", интернатура";
			}
			if (intern.getTraineeship()) {
				internAndTrain = internAndTrain.isEmpty() ? "ординатура" : internAndTrain + ", ординатура";
			}
		});
		setValue(internship, internAndTrain);
		
		currentDegree = "";
		employee.getEducation().getDegrees().forEach(d -> {
			if (d.getKmn() && currentDegree.isEmpty()) {
				currentDegree = "к.м.н";
			}
			if (d.getDmn()) {
				currentDegree = "д.м.н";
			}
		});
		setValue(degree, currentDegree);
		
		setValue(certificates, String.valueOf(employee.getEducation().getCertificates().size()));
		setValue(qualifications, String.valueOf(employee.getEducation().getQualifications().size()));
		setValue(validations, String.valueOf(employee.getEducation().getValidations().size()));
		setValue(retrainings, String.valueOf(employee.getEducation().getRetrainings().size()));
	}
	
	private void setValue(Label label, String value) {
		String currentValue = label.getText();
		int endIndex = currentValue.indexOf(":");
		String text = currentValue.substring(0, endIndex);
		label.setText(text + ": " + value);
		label.pack();
	}
	
	

}
